﻿using System;
using System.Collections.Generic;

namespace Lucene.Linq.Utility
{
    internal static class FunctionalExtensions
    {
        internal static Func<TA, TR> Memoize<TA, TR>(this Func<TA, TR> f)
        {
            var map = new Dictionary<TA, TR>();
            return a =>
                       {
                           TR value;
                           if (map.TryGetValue(a, out value))
                               return value;
                           value = f(a);
                           map.Add(a, value);
                           return value;
                       };
        }
    }
}